package com.eqgis.eqtool.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.eqgis.eqtool.R;
import com.eqgis.eqtool.chart.base.BaseChart;
import com.eqgis.eqtool.chart.bean.ChartBean;

import java.util.List;

/**
 * 水平柱状图
 * @desc 用于实时展示UAV地类统计结果
 * @Created by tyx 2021/09/23
 */
public class HorizontalChartBar extends BaseChart {
    private List<ChartBean> datas;
    private float startX, endX, startY, endY;
    private Paint basePaint, baseLabelPaint, xyPaint, hintPaint, rectPaint,rectPaint2;

    private float maxValue = 4000f;
    private int maxValueSegment = 4;
//    private int spacingDistance = 0;//单位dp
    private int[] colorId;//颜色//左矩形，右矩形，文字颜色，线条颜色
    private float leftPadding = 28;//单位dp

    public HorizontalChartBar(Context context) {
        super(context);
    }

    public HorizontalChartBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HorizontalChartBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init(Context context, AttributeSet attrs) {
        super.init(context, attrs);
        initPaint();
    }
    private void initPaint() {
        basePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        basePaint.setColor(Color.WHITE);
        basePaint.setStrokeWidth(dip2px(0.5f));
        basePaint.setTextSize(dip2px(10));
        basePaint.setTextAlign(Paint.Align.RIGHT);
        basePaint.setStrokeCap(Paint.Cap.ROUND);
        basePaint.setDither(true);

        baseLabelPaint = new Paint();
        baseLabelPaint.setColor(ContextCompat.getColor(getContext(), R.color.black));
        baseLabelPaint.setTextSize(dip2px(12));
        baseLabelPaint.setTextAlign(Paint.Align.LEFT);
        Typeface font0 = Typeface.create(Typeface.SANS_SERIF, Typeface.DEFAULT_BOLD.getStyle());
        baseLabelPaint.setTypeface(font0);

        xyPaint = new Paint(basePaint);
        xyPaint.setColor(Color.WHITE);
        xyPaint.setStrokeWidth(dip2px(1));

        hintPaint = new Paint(basePaint);
        hintPaint.setStrokeWidth(1.5f);
    }

    public float getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(float maxValue) {
        this.maxValue = maxValue;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            startX = getPaddingLeft();
            endX = getMeasuredWidth() - getPaddingRight();
            startY = getMeasuredHeight() - getPaddingBottom();
            endY = getPaddingTop();
        }
    }

    public HorizontalChartBar setRectData(List<ChartBean> datas) {
        this.datas = datas;
        return this;
    }

    public HorizontalChartBar setColorId(int... colorId) {
        this.colorId = colorId;
        return this;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawLine(canvas);//画横线

//        if (mAnimatedValue == 0) return;
        drawNoTouch(canvas);
        super.onDraw(canvas);
    }

    private void drawLine(Canvas canvas) {
        //水平轴
        canvas.drawLine(startX + getRectLeft(1.2f * leftPadding), startY-getDx() * 0.6f, endX, startY-getDx() * 0.6f, hintPaint);
        //左侧竖直向的轴
        canvas.drawLine(startX + getRectLeft(1.2f * leftPadding), startY-getDx() * 0.6f,
                startX + getRectLeft(1.2f * leftPadding), endY, hintPaint);
//        //顶部的横线
//        canvas.drawLine(startX, endY, endX, endY, hintPaint);
    }
    private void drawNoTouch(Canvas canvas) {
        if (datas == null || datas.size() == 0 || xyPaint == null) return;
        drawX_Tag(canvas); //画X轴tag
        drawY_Tag(canvas); //画X轴tag
        //drawY(canvas); //画Y轴
        drawRect(canvas);//画矩形
    }
    private void drawX_Tag(Canvas canvas) {
        float dx = getDx();
        xyPaint.setTextSize(getDx() * 0.6f);

        for (int i = 0; i < datas.size(); i++) {
            canvas.drawText(datas.get(i).getTag(), getRectLeft(leftPadding),(float) (startY - dx * (2 * i+1.36f)), xyPaint);
        }
    }
    private void drawY_Tag(Canvas canvas) {
        float dy =  (endX - getRectLeft(leftPadding * 1.2f))/ maxValueSegment;
        Paint yTagPaint = new Paint(xyPaint);
        yTagPaint.setTextAlign(Paint.Align.CENTER);
        for (int i = 0; i < maxValueSegment; i++) {
            canvas.drawLine(startX+ dip2px(leftPadding*1.2f) + dy * i ,startY-getDx() * 0.6f,
                    startX+ dip2px(leftPadding*1.2f) + dy * i,startY-getDx() * 0.72f ,hintPaint);
            canvas.drawText(String.valueOf(Math.round(maxValue / maxValueSegment * i)), startX+ dip2px(leftPadding*1.2f) + dy * i ,startY, yTagPaint);
        }
    }
    private void drawRect(Canvas canvas) {
        float dx = getDx();
        float dy = (endX - startX - dip2px(leftPadding)) / maxValue;
        rectPaint = new Paint(basePaint);
        rectPaint.setColor(ContextCompat.getColor(getContext(), colorId[0]));
        boolean drawRect2 = true;

        try {
            rectPaint2 = new Paint(basePaint);
            rectPaint2.setColor(ContextCompat.getColor(getContext(), colorId[1]));
        }catch (NullPointerException e){
            drawRect2 = false;
        }

        for (int i = 0; i < datas.size(); i++) {
            RectF rectf = new RectF(getRectLeft(leftPadding * 1.2f),startY - dx * (2 * i + 2),
                    datas.get(i).getValue() * dy + startX+ dip2px(leftPadding*1.2f),startY - dx * (2 * i + 1));
            canvas.drawRect(rectf, rectPaint);
            if (drawRect2){
                RectF rectf2 = new RectF(datas.get(i).getValue() * dy + startX+ dip2px(leftPadding*1.2f),
                        startY - dx * (2 * i + 2),
                        endX,startY - dx * (2 * i + 1));
                canvas.drawRect(rectf2, rectPaint2);
            }
        }
    }

    private float getRectLeft(float v) {
        return startX + dip2px(v);
    }

    /**
     * 计算 竖向x 轴 平均值
     *
     * @return
     */
    private float getDx() {
        return 0.5f * (startY - endY) / datas.size();
    }

    @Override
    public void start() {
        this.postInvalidate();
    }
}
